<template>
  <div 
    ref="cardRef"
    class="card-3d-container"
    @mouseenter="onMouseEnter"
    @mouseleave="onMouseLeave"
    @click="onCardClick"
  >
    <div class="card-3d" :class="{ flipped: isFlipped }">
      <!-- 正面 -->
      <div class="card-face card-front">
        <div class="card-header">
          <div class="card-icon">
            <slot name="icon">
              <div class="default-icon"></div>
            </slot>
          </div>
          <div class="card-title-section">
            <h3 class="card-title">{{ title }}</h3>
            <p class="card-subtitle">{{ subtitle }}</p>
          </div>
        </div>
        <div class="card-content">
          <slot name="content"></slot>
        </div>
        <div class="card-footer">
          <slot name="footer"></slot>
        </div>
      </div>
      
      <!-- 背面 -->
      <div class="card-face card-back">
        <div class="card-back-content">
          <slot name="back-content">
            <div class="default-back-content">
              <h4>详细信息</h4>
              <p>{{ backContent || '点击查看更多详细信息...' }}</p>
            </div>
          </slot>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { gsap } from 'gsap'

interface Props {
  title: string
  subtitle?: string
  backContent?: string
  delay?: number
}

const props = withDefaults(defineProps<Props>(), {
  subtitle: '',
  backContent: '',
  delay: 0
})

const cardRef = ref<HTMLElement>()
const isFlipped = ref(false)

const onMouseEnter = () => {
  if (!cardRef.value) return
  
  gsap.to(cardRef.value.querySelector('.card-3d'), {
    rotationX: 5,
    rotationY: 5,
    scale: 1.02,
    duration: 0.3,
    ease: "power2.out"
  })
  
  gsap.to(cardRef.value, {
    boxShadow: "0 20px 40px rgba(96, 165, 250, 0.3)",
    duration: 0.3,
    ease: "power2.out"
  })
}

const onMouseLeave = () => {
  if (!cardRef.value) return
  
  gsap.to(cardRef.value.querySelector('.card-3d'), {
    rotationX: 0,
    rotationY: 0,
    scale: 1,
    duration: 0.3,
    ease: "power2.out"
  })
  
  gsap.to(cardRef.value, {
    boxShadow: "0 8px 24px rgba(0, 0, 0, 0.1)",
    duration: 0.3,
    ease: "power2.out"
  })
}

const onCardClick = () => {
  if (!cardRef.value) return
  
  // 按压效果
  gsap.to(cardRef.value.querySelector('.card-3d'), {
    scale: 0.95,
    duration: 0.1,
    ease: "power2.out",
    onComplete: () => {
      // 翻转效果
      isFlipped.value = !isFlipped.value
      gsap.to(cardRef.value!.querySelector('.card-3d'), {
        scale: 1,
        duration: 0.2,
        ease: "back.out(1.7)"
      })
    }
  })
}

onMounted(() => {
  if (!cardRef.value) return
  
  // 入场动画
  gsap.fromTo(cardRef.value, 
    {
      opacity: 0,
      y: 50,
      rotationX: -15
    },
    {
      opacity: 1,
      y: 0,
      rotationX: 0,
      duration: 0.8,
      delay: props.delay,
      ease: "back.out(1.7)"
    }
  )
})
</script>

<style scoped>
.card-3d-container {
  perspective: 1200px;
  width: 320px;
  height: auto;
  min-height: 280px;
  margin: 16px;
  cursor: pointer;
  will-change: transform, box-shadow;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  border-radius: 16px;
  transition: box-shadow 0.3s ease;
}

@media (max-width: 768px) {
  .card-3d-container {
    width: 280px;
    margin: 12px;
  }
}

.card-3d {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.6s cubic-bezier(0.4, 0.0, 0.2, 1);
  will-change: transform;
}

.card-3d.flipped {
  transform: rotateY(180deg);
}

.card-face {
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  border-radius: 16px;
  background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
  border: 1px solid rgba(148, 163, 184, 0.1);
  padding: 24px;
  box-sizing: border-box;
}

.card-back {
  transform: rotateY(180deg);
  background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
  color: white;
}

.card-header {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 20px;
}

.card-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: linear-gradient(135deg, #93c5fd 0%, #3b82f6 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.default-icon {
  width: 24px;
  height: 24px;
  background: white;
  border-radius: 4px;
}

.card-title-section {
  flex: 1;
}

.card-title {
  font-size: 20px;
  font-weight: 600;
  color: #1e293b;
  margin: 0 0 4px 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

.card-subtitle {
  font-size: 14px;
  color: #64748b;
  margin: 0;
  font-weight: 400;
}

.card-content {
  flex: 1;
  margin-bottom: 20px;
}

.card-footer {
  margin-top: auto;
}

.card-back-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.default-back-content h4 {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 12px 0;
  color: #f1f5f9;
}

.default-back-content p {
  font-size: 14px;
  color: #cbd5e1;
  margin: 0;
  line-height: 1.6;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .card-3d-container {
    perspective: 800px;
  }
  
  .card-title {
    font-size: 18px;
  }
  
  .card-subtitle {
    font-size: 13px;
  }
  
  .card-icon {
    width: 40px;
    height: 40px;
  }
  
  .card-face {
    padding: 20px;
  }
}

@media (max-width: 480px) {
  .card-3d-container {
    perspective: 600px;
  }
}
</style>